﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Web.Script.Serialization;
namespace YZZ
{

    class FileHelperTemplate
    {
        static public string ReadFile(string PathFilename)
        {
            string result = string.Empty;
            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(PathFilename))
                {
                    result = file.ReadToEnd();
                }
            }
            catch
            {
                //MessageBox.Show("读取时遇到错误，写入失败。可能是磁盘已满或者没有写入权限", "数据保存");
            }
            return result;
        }
        static public void RemoveFile(string PathFilename)
        {
            if (File.Exists(PathFilename))
            {
                File.Delete(PathFilename);
            }
        }
        static public string[] ListDirectoryFiles(string Path)
        {
            return Directory.GetFiles(Path);
        }
        static public bool HasFile(string PathFilename)
        {
            return File.Exists(PathFilename);
        }
        static public void CopyFile(string PathFilename, string ToPathFilename)
        {
            if (File.Exists(PathFilename))
            {
                File.Copy(PathFilename, ToPathFilename, true);
            }
        }
        static public void RemoveDirectory(string Path)
        {
            if (Directory.Exists(Path))
            {
                try
                {
                    //去除文件夹和子文件的只读属性
                    //去除文件夹的只读属性
                    System.IO.DirectoryInfo fileInfo = new DirectoryInfo(Path);
                    fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                    //去除文件的只读属性
                    System.IO.File.SetAttributes(Path, System.IO.FileAttributes.Normal);

                    //判断文件夹是否还存在
                    if (Directory.Exists(Path))
                    {
                        foreach (string f in Directory.GetFileSystemEntries(Path))
                        {
                            if (File.Exists(f))
                            {
                                //如果有子文件删除文件
                                File.Delete(f);
                                Console.WriteLine(f);
                            }
                            else
                            {
                                //循环递归删除子文件夹
                                RemoveDirectory(f);
                            }
                        }

                        //删除空文件夹
                        Directory.Delete(Path);
                        //Console.WriteLine(Path);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("清理过程中遇到错误：" + e.Message);
                }

            }
        }
        static public void WriteFile(string PathFilename, string output)
        {
            string currentLine = string.Empty;
            try
            {
                if (File.Exists(PathFilename))
                {
                    File.Delete(PathFilename);
                }
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(PathFilename))
                {
                    file.WriteLine(output);
                }
            }
            catch
            {
                MessageBox.Show("写入时遇到错误，写入失败。可能是磁盘已满或者没有写入权限", "数据保存");
            }
        }

    }
}
